package com.work.formaldehyde.activityadapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.KeFuActivity;
import com.work.formaldehyde.activity.LoginActivity;
import com.work.formaldehyde.activity.QueRenDingDanActivity;
import com.work.formaldehyde.activity.SetWechatActivity;
import com.work.formaldehyde.activity.ShangPingActivity;
import com.work.formaldehyde.model.ShangPingModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class JianCeListAdapter extends BaseAdapter {
    ArrayList<ShangPingModel.data> list;
    Context context;
    Activity activity;

    public JianCeListAdapter(ArrayList<ShangPingModel.data> list, Context context, Activity activity) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.jiance_list_item, null);
        init i = new init();
        i.homeitem_text = convertView.findViewById(R.id.homeitem_text);
        i.homeitem_type = convertView.findViewById(R.id.homeitem_type);
        i.manjian_text = convertView.findViewById(R.id.manjian_text);
        i.money = convertView.findViewById(R.id.money);
        i.pp_text = convertView.findViewById(R.id.pp_text);
        i.jiance_item_img = convertView.findViewById(R.id.jiance_item_img);
        i.hot_text = convertView.findViewById(R.id.hot_text);
        i.manjian = convertView.findViewById(R.id.manjian);
        i.now_yuyue = convertView.findViewById(R.id.now_yuyue);
        i.zixun=convertView.findViewById(R.id.zixun);
        i.homeitem_text.setText(list.get(position).getGoods_name());
        ApiUtils.GET_IMAGE(context, list.get(position).getGoods_img(), i.jiance_item_img);
        i.money.setText("￥" + list.get(position).getGoods_discount_price());
        i.homeitem_text.setText(list.get(position).getGoods_name());
        i.pp_text.setText("已有" + list.get(position).getGoods_yy() + "人预约");
        i.manjian_text.setText(list.get(position).getGoods_discount());
        i.homeitem_type.setText("累计检测" + list.get(position).getGoods_lj() + "个家庭");
        i.zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent tokf = new Intent(activity, KeFuActivity.class);
                activity.startActivity(tokf, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
            }
        });
        i.now_yuyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Url.USER_ID.equals("")) {
                    Intent is_login = new Intent(context, LoginActivity.class);
                    context.startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                    ApiUtils.SetToast(context, "请先登录");
                } else {
                    Intent qrdd = new Intent(context, QueRenDingDanActivity.class);
                    qrdd.putExtra("shangpingid", "" + list.get(position).getId());
                    qrdd.putExtra("shangpingmoney", "" + list.get(position).getGoods_discount_price());
                    qrdd.putExtra("shangpingimg", "" +list.get(position).getGoods_img());
                    qrdd.putExtra("dingdan", "false");
                    qrdd.putExtra("allmoney", "");
                    if (list.get(position).getIs_day().equals("0"))
                        qrdd.putExtra("suantianshu", "0");
                    else
                        qrdd.putExtra("suantianshu", "1");
                    context.startActivity(qrdd, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                    Map<String, Object> music = new HashMap<String, Object>();
                    music.put("立即预约", "1");//自定义参数：音乐类型，值：流行
                    MobclickAgent.onEventObject(context, "now_appointment", music);
                }
            }
        });
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView homeitem_text, homeitem_type, manjian_text, money, pp_text;
        ImageView jiance_item_img;
        RelativeLayout hot_text, manjian, now_yuyue,zixun;
    }
}

